//
//  GB2ShapeCache.h
//  
//  Loads physics sprites created with http://www.PhysicsEditor.de
//
//  Generic Shape Cache for box2d
//
//  Copyright by Andreas Loew 
//      http://www.PhysicsEditor.de
//      http://texturepacker.com
//      http://www.code-and-web.de
//  
//  All rights reserved.
//
//  Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
//  
//  The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
//  
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
//
#pragma once
#include "cocos2d.h"
#include "Box2D/Box2D.h"

#define GB2_FOREACH_DICT(__array__, __object__)												\
    if (__array__ && __array__->count() > 0)													\
	for(CCMutableArrayIterator it = __array__->begin(); it != __array__->end()				\
		&& ((__object__ = (CCDictionary<std::string, CCObject*>*)*it)) != NULL; ++it)		

#define GB2_FOREACH_ARRAY(__array__, __object__)												\
    if (__array__ && __array__->count() > 0)															\
	for(CCMutableArrayIterator it = __array__->begin(); it != __array__->end()				\
		&& ((__object__ = (CCMutableArray<>*)*it)) != NULL; ++it)	

#define GB2_FOREACH_STR(__array__, __object__)												\
    if (__array__ && __array__->count() > 0)									\
	for(CCMutableArrayIterator it = __array__->begin(); it != __array__->end()				\
		&& ((__object__ = (CCString*)*it)) != NULL; ++it)	
using namespace cocos2d;
/**
 * Shape cache 
 * This class holds the shapes and makes them accessible 
 * The format can be used on any MacOS/iOS system
 */
class BodyDef;
class GB2ShapeCache : public CCObject 
{
	CCMutableDictionary<std::string, BodyDef*> *shapeObjects_;
    //NSMutableDictionary *shapeObjects_;
    float ptmRatio_;

	static CCPoint CGPointFromString(std::string str);

public:
	static GB2ShapeCache* sharedShapeCache();
	typedef std::map<std::string, CCObject*>	CCObjectMap;
	typedef CCObjectMap::iterator	CCObjectMapIter;
	typedef std::vector<CCObject*>	CCObjectArray;
	typedef CCObjectArray::iterator	CCMutableArrayIterator;
	~GB2ShapeCache();
	bool init();
	/**
	 * Adds shapes to the shape cache
	 * @param plist name of the plist file to load
	 */
	 void addShapesWithFile(std::string plist);

	/**
	 * Adds fixture data to a body
	 * @param body body to add the fixture to
	 * @param shape name of the shape
	 */
	void addFixturesToBody(b2Body *body, std::string shape);

	/**
	 * Returns the anchor point of the given sprite
	 * @param shape name of the shape to get the anchorpoint for
	 * @return anchorpoint
	 */
	CCPoint anchorPointForShape(std::string shape);

	/**
	 * Returns the ptm ratio
	 */
	float ptmRatio(){ return ptmRatio_;};

};



/**
 * Internal class to hold the fixtures
 */
class FixtureDef 
{
public:
    FixtureDef(): next(0)
    {}
    
    ~FixtureDef()
    {
        delete next;
        delete fixture.shape;
    }
    
    FixtureDef *next;
    b2FixtureDef fixture;
    int callbackData;
};

/**
 * Body definition
 * Holds the body and the anchor point
 */
class BodyDef : public CCObject
{
public :
    FixtureDef *fixtures;
    CCPoint anchorPoint;
	bool init();
	~BodyDef();
};